package com.ulrica.morning.tools.tcmsp;

import com.alibaba.fastjson2.JSON;
import com.ulrica.morning.dto.tcmsp.Ingredients;
import com.ulrica.morning.dto.tcmsp.LatinName;
import com.ulrica.morning.dto.tcmsp.Targets;
import com.ulrica.morning.easyexcel.WriteFile;
import lombok.SneakyThrows;
import org.jsoup.Connection;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.math.BigDecimal;
import java.util.Arrays;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * 酒精性脂肪肝:
 * 葛花、木香、砂仁、茯苓、猪苓、人参、白术、白豆蔻、青皮、陈皮、神曲、干姜、泽泻
 *
 * @author LongKui
 * @since 2022/06/28
 */
public class GrabTCMSP {
    private static final String URI = "https://old.tcmsp-e.com/tcmspsearch.php?qr=%s&qsr=herb_en_name&token=71c3de2323b5cabf70be678a64c9aff5";
    private static final String START_WITH = "data: ";
    private static final String PATH = "D:\\longkui\\Desktop\\网络药理学_2021\\药品信息\\%s.txt";

    public static void getDrugInfoList(Set<LatinName> latinNameSet) {
        for (LatinName latinName : latinNameSet) {
            getDrugInfo(latinName);
        }
    }

    @SneakyThrows
    private static void getDrugInfo(LatinName latinName) {
        String url = String.format(URI, latinName.getHerb_en_name());
        Connection connect = Jsoup.connect(url);
        Document doc = connect.get();
        Elements elements = doc.getElementsByTag("script");
        Element element = elements.get(11);
        String data = element.data();
        System.out.println("=========================================");

        int start = data.indexOf("$(\"#grid\").kendoGrid({");
        int end = data.indexOf("});");
        String grid = data.substring(start, end);
        getIngredients(grid, latinName.getHerb_pinyin());

        System.out.println("=========================================");

        data = data.substring(end + 1);
        start = data.indexOf("$(\"#grid2\").kendoGrid({");
        end = data.indexOf("});");
        String grid2 = data.substring(start, end);
        getTargets(grid2, latinName.getHerb_pinyin());

        System.out.println("=========================================");
    }

    private static void getIngredients(String grid, String pinyin) {
        String[] gridArr = grid.split("\r\n");
        String data = Arrays.stream(gridArr).filter(s -> s.trim().startsWith(START_WITH)).collect(Collectors.joining());
        String content = data.substring(data.indexOf("["), data.length() - 1);
        List<Ingredients> list = JSON.parseArray(content, Ingredients.class);
        list = list.stream().filter(ingredients -> {
            BigDecimal ob = new BigDecimal(ingredients.getOb());
            BigDecimal dl = new BigDecimal(ingredients.getDl());
            return ob.compareTo(new BigDecimal(30)) >= 0
                    && dl.compareTo(BigDecimal.valueOf(0.18)) >= 0;
        }).collect(Collectors.toList());
        System.out.println("Ingredients: " + list.size());
        write(list, String.format("%s_ingredients", pinyin));
    }

    private static void getTargets(String grid, String pinyin) {
        String[] gridArr = grid.split("\r\n");
        String data = Arrays.stream(gridArr).filter(s -> s.trim().startsWith(START_WITH)).collect(Collectors.joining());
        String content = data.substring(data.indexOf("["), data.length() - 1);
        List<Targets> list = JSON.parseArray(content, Targets.class);
        System.out.println("Targets: " + list.size());
        write(list, String.format("%s_targets", pinyin));
    }

    private static void write(List<?> list, String name) {
        String fileName = String.format("%s_targets", name);
        WriteFile.writeTxt(list, String.format(PATH, fileName));
    }
}
